package fr.esiea.es_inf5042.gui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class BargainLocationItemOverlay extends ItemizedOverlay<OverlayItem>{

	Context context;
	private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
	Drawable marker;
	
	public BargainLocationItemOverlay(Drawable m, Context c) {
		super(boundCenterBottom(m));
		this.marker = m;
		marker.setBounds(-marker.getIntrinsicWidth()/2, -marker.getIntrinsicHeight(), marker.getIntrinsicWidth()/2,0);
		context = c;
	}

	public void addOverlay(OverlayItem overlay) {
		overlays.add(overlay);
	    populate();
	}
	
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = overlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}
	
	@Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
      super.draw(canvas, mapView, shadow);
      boundCenterBottom(marker);
      //marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
    }
	
	@Override
	protected OverlayItem createItem(int i) {
		return overlays.get(i);
	}

	@Override
	public int size() {
		return overlays.size();
	}

}

